from turtle import *
import time
import _thread
import os
ht()
speed(0)
penup()

from socket import *
alice=input('connect to:')
bob=socket(AF_INET,SOCK_STREAM)
bob.connect((alice,12345))

n=int(bob.recv(10).decode('ASCII'))
m=int(bob.recv(10).decode('ASCII'))

def P(x,y):
    return (400/m*y-200,-400/n*x+200)
def IP(x,y):
    return (int((200-y)*n/400+0.5),int((200+x)*m/400+0.5))
edge=[]
sqr=[]
for i in range(n+2):
    edge.append([])
    sqr.append([])
    for j in range(m+2):
        sqr[i].append(0)
        edge[i].append([0,0])
        if i==n: edge[i][j][0]=-1
        if j==m: edge[i][j][1]=-1
        #write(f'{i},{j}')
for i in range(n+1):
    for j in range(m+1):
        goto(P(i,j))
        dot(5)

cnt=[0,0,0]
def gameover():
    #print(edge)
    for i in range(n+1):
        for j in range(m+1):
            if edge[i][j][0] == 0 or edge[i][j][1] == 0:
                return False
    return True

def draw(x1,y1,x2,y2,col):
    if x1>x2 or y1>y2:
        t=x1
        x1=x2
        x2=t
        t=y1
        y1=y2
        y2=t
    if x2-x1==1:
        if edge[x1][y1][0]:return True
        edge[x1][y1][0]=col
        sqr[x1+1][y1+1]+=1
        sqr[x1+1][y1]+=1
        if(sqr[x1+1][y1+1]==4): cnt[col]+=1
        if(sqr[x1+1][y1]==4): cnt[col]+=1
        bob.send(f'{x1},{y1},0'.encode('ASCII'))
    if y2-y1==1:
        if edge[x1][y1][1]:return True
        edge[x1][y1][1]=col
        sqr[x1+1][y1+1]+=1
        sqr[x1][y1+1]+=1
        if(sqr[x1+1][y1+1]==4): cnt[col]+=1
        if(sqr[x1][y1+1]==4): cnt[col]+=1
        bob.send(f'{x1},{y1},1'.encode('ASCII'))
    return False

def end():
    if(cnt[1]>cnt[2]): print('Alice win')
    elif(cnt[1]<cnt[2]): print('Bob win')
    else: print('draw')
    os.system('pause')
    exit(0)

turn=0
clct=0
la=0
lb=0
def enemyturn():
    print('enemy turn')
    x,y,z=bob.recv(30).decode('ASCII').split(',')
    x=int(x)
    y=int(y)
    z=int(z)
    x1,y1=P(x,y)
    x2,y2=P(x+(z+1)%2,y+z)
    color('red')
    goto(x1,y1)
    dot(6)
    pd()
    goto(x2,y2)
    dot(6)
    pu()
    edge[x][y][z]=1
    sqr[x+1][y+1]+=1
    sqr[x+1][y]+=1
    if(sqr[x+1][y+1]==4): cnt[1]+=1
    if(sqr[x+1][y]==4): cnt[1]+=1
    global clct
    clct=2
    if gameover(): end()
def clc(x,y):
    global clct
    global lb
    global la
    a,b=IP(x,y)
    x,y=P(a,b)
    if clct==2:
        color('blue')
        goto(x,y)
        dot(6)
        pd()
        la=a
        lb=b
    elif clct==3:
        if abs(a-la)+abs(b-lb)!=1:return
        if draw(a,b,la,lb,2):return
        goto(x,y)
        dot(6)
        pu()
    else:
        #print('enemy turn')
        return
    if gameover(): end()
    if clct==3: _thread.start_new_thread(enemyturn,())
    clct=(clct+1)%4

_thread.start_new_thread(enemyturn,())
onscreenclick(clc)
done()
